<template>
  <view class="root">
    <view class="top-wrapper">
      <view class="content-inner">
        <template v-if="routerParams.source === 'no-pass'">
          <image class="pass-img" :src="baseImgUrl +'no-pass-icon.png'"></image>
          <view class="pass-text" style="color: #EB0101;">
            本次检查不合格
          </view>
        </template>
        <template v-else>
          <image class="pass-img" :src="baseImgUrl +'check-done-pass.png'"></image>
          <view class="pass-text">
            本次检查合格
          </view>
        </template>
        <view class="sign-text">
          请商户负责人签字确认:
        </view>

        <view class="sign-button" v-if="!signResult.fullPath" @click="handleSign">
          签名
        </view>
        <template v-else>
          <image :src="signResult.fullPath" class="sign-img-done" @click="handleSign"></image>
          <view class="again-sign-button" @click="handleSign">
            重新签名
          </view>
        </template>
      </view>
    </view>
    <view class="bottom-wrapper">
      <u-button class="custom-style" text="完成检查" @click="handleCheckDone"
        :custom-style="customSubmitStyleFull"></u-button>
    </view>
  </view>
</template>

<script>
  import {
    inspectInspectionItems,
    setInspectionItems,
    submitInspectInspectionItems
  } from '../api/api'
  import {
    Toast,
    customSubmitStyleFull,
    getOptionsParams,
    isLoading,
    navTo
  } from '@/utils/util.js'
  import {
    mapState
  } from 'vuex'
  import _ from '@/utils/lodash.js'

  export default {
    data() {
      return {
        baseImgUrl: this.$baseImgUrl,
        customSubmitStyleFull,
        signResult: {},
        routerParams: {}
      }
    },
    onLoad(options) {
      this.routerParams = getOptionsParams(options)
      console.log(this.routerParams)
      uni.$on('signed', (data) => {
        this.signResult = data
      })
    },
    computed: {
      ...mapState(['policeCheckboxValue', 'crimeImg', 'radiovalue', 'evidenceList'])
    },
    onUnload() {
      uni.$off('signed')
    },
    methods: {
      handleCheckDone() {
        if (!this.signResult.fullPath) {
          Toast('请完成签名！')
          return
        }
        // 给每个结果后面挂证据内容，然后拍平
        let params = {}
        const generateIndustryExamineDetailList = () => {
          const industryExamineDetailList = []
          // 给每个结果后面挂证据内容，然后拍平
          this.radiovalue.forEach((itemArr, i) => {
            itemArr.forEach((item, index) => {
              let temp = {}
              if (this.routerParams.source === 'no-pass') {
                temp = {
                  ...item,
                  passes: item.value,
                }
                const checkMemo = _.get(this.evidenceList, `[${i}][${index}].value`)
                const path = _.get(this.evidenceList, `[${i}][${index}].imgUrl`, [])
                if (checkMemo) {
                  temp.checkMemo = checkMemo
                }
                if (path && path.length) {
                  temp.path = path.map(item => item.halfPath)
                    .join(',')
                }
              } else {
                temp = {
                  ...item,
                  passes: item.value,
                }
              }
              temp.checkTaskId = this.routerParams.id
              temp.principalSignatureUrl = this.signResult.halfPath
              temp.rectificationType = this.routerParams.rectificationType
              temp.penaltyEndTime = this.routerParams.penaltyEndTime ? new Date(this.routerParams
                  .penaltyEndTime)
                .getTime() : this
                .routerParams.penaltyEndTime;
              temp.policeUserIds = this.policeCheckboxValue.join(',')
              industryExamineDetailList.push(temp)
            })
          })
          return industryExamineDetailList
        }
        // 生成组装数据的函数
        // 如果是复查  ，可能分复查通过，复查不通过 , 复查或者检查任务
        if (['recheck'].includes(this.routerParams.action)) {
          params = {}
          // 完成检查组装数据
          const industryExamineDetailList = generateIndustryExamineDetailList()
          isLoading(true)
          submitInspectInspectionItems(industryExamineDetailList)
            .then((res) => {
              Toast('操作成功！')
              // 清空数据
              this.$store.commit('INIT_RADIOVALUE', [])
              this.$store.commit('INIT_EVIDENCE_LIST', [])
              this.$store.commit('SET_POLICE_CHECKBOX_VALUE', [])
              this.$store.commit('SET_CRIME_IMG', '')
              setTimeout(() => {
                uni.switchTab({
                  url: '/pages/home/home'
                })
              }, 500)
            })
            .finally(() => {
              isLoading(false)
            })
        } else if (this.routerParams.action === 'inspectionTask') {
          params = generateIndustryExamineDetailList()
          // 检查任务           
          submitInspectInspectionItems(params)
            .then((res) => {
              Toast('操作成功！')
              // 清空数据
              this.$store.commit('INIT_RADIOVALUE', [])
              this.$store.commit('INIT_EVIDENCE_LIST', [])
              this.$store.commit('SET_POLICE_CHECKBOX_VALUE', [])
              this.$store.commit('SET_CRIME_IMG', '')
              setTimeout(() => {
                uni.switchTab({
                  url: '/pages/home/home'
                })
              }, 500)
            })
            .finally(() => {
              isLoading(false)
            })
        } else {
          // 完成检查组装数据
          const industryExamineDetailList = []
          this.radiovalue.forEach((itemArr, i) => {
            itemArr.forEach((item, index) => {
              let temp = {}
              if (this.routerParams.source === 'no-pass') {
                temp = {
                  ...item,
                  passes: item.value,
                }
                const checkMemo = _.get(this.evidenceList, `[${i}][${index}].value`)
                const path = _.get(this.evidenceList, `[${i}][${index}].imgUrl`, [])
                if (checkMemo) {
                  temp.checkMemo = checkMemo
                }
                if (path && path.length) {
                  temp.path = path.map(item => item.halfPath)
                    .join(',')
                }
              } else {
                temp = {
                  ...item,
                  passes: item.value,
                }
              }
              industryExamineDetailList.push(temp)
            })
          })
          // 检查
          params = {
            companyId: this.routerParams.companyId,
            // 民辅警
            policeUserId: this.policeCheckboxValue.join(','),
            // 现场照片
            signPicture: this.crimeImg,
            // 检查项内容
            industryExamineDetailList: JSON.stringify(industryExamineDetailList),
            principalSignatureUrl: this.signResult.halfPath,
          }
          // 检查不通过
          if (this.routerParams.source === 'no-pass') {
            params.rectificationType = this.routerParams.rectificationType
            params.penaltyEndTime = this.routerParams.penaltyEndTime ? new Date(this.routerParams.penaltyEndTime)
              .getTime() : this
              .routerParams.penaltyEndTime
          }
          isLoading(true)
          setInspectionItems(params)
            .then((res) => {
              if (res.code == 200) {
                Toast('操作成功！')
                // 清空数据
                this.$store.commit('INIT_RADIOVALUE', [])
                this.$store.commit('INIT_EVIDENCE_LIST', [])
                this.$store.commit('SET_POLICE_CHECKBOX_VALUE', [])
                this.$store.commit('SET_CRIME_IMG', '')
                setTimeout(() => {
                  uni.switchTab({
                    url: '/pages/home/home'
                  })
                }, 500)
              } else {}
            })
            .finally(() => {
              isLoading(false)
            })
        }
      },
      handleSign() {
        navTo({
          url: '/venueInspection/sign'
        })
      }
    }
  }
</script>

<style>
  page {
    width: 100%;
    height: 100%;
  }
</style>

<style lang="scss" scoped>
  .root {
    display: flex;
    width: 100%;
    height: 100%;
    flex-direction: column;
    background: #F9F9F9;
    padding: 0 24rpx;



    .top-wrapper {
      flex: 1;
      overflow: auto;
      margin: 10rpx 0;
      background: #FFFFFF;
      border-radius: 16rpx 16rpx 16rpx 16rpx;

      .content-inner {
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;

        .pass-img {
          width: 91rpx;
          height: 91rpx;
          margin-top: 36rpx;
          margin-bottom: 22rpx;
        }

        .pass-text {
          font-weight: 500;
          font-size: 36rpx;
          color: #0CAD83;
          margin-bottom: 58rpx;
        }

        .sign-text {
          font-weight: 500;
          font-size: 28rpx;
          color: #333333;
          margin-bottom: 26rpx;
        }

        .sign-img-done {
          width: 560rpx;
          height: 210rpx;
          background: #F9F9F9;
          border-radius: 16rpx 16rpx 16rpx 16rpx;
        }

        .again-sign-button {
          width: 240rpx;
          height: 88rpx;
          background: #DCEDFF;
          border-radius: 44rpx 44rpx 44rpx 44rpx;
          border: 1rpx solid #DCEDFF;
          font-weight: 500;
          font-size: 36rpx;
          color: #2488F5;
          margin-top: 24rpx;
          display: flex;
          align-items: center;
          justify-content: center;
        }

        .sign-button {
          width: 240rpx;
          height: 88rpx;
          background: #DCEDFF;
          border-radius: 44rpx 44rpx 44rpx 44rpx;
          border: 1rpx solid #DCEDFF;
          font-weight: 500;
          font-size: 36rpx;
          color: #2488F5;
          display: flex;
          align-items: center;
          justify-content: center;
        }
      }
    }

    .bottom-wrapper {
      width: 100%;
      height: 120rpx;
      background: #FFFFFF;
      display: flex;
      align-items: center;
      justify-content: center;
    }
  }
</style>